﻿using Dapper;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using report_yx.Helpers;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace report_yx.Controllers
{
    [ApiController]
    [Route("api")]
    public class ApiController : Controller
    {
        private DataContext dataContext;
        public ApiController(DataContext dataContext)
        {
            this.dataContext = dataContext;
        }

        [HttpPost]
        [Route("ec")]
        public IActionResult Query([FromBody] dynamic entity)
        {
            //var command = $"P_{entity.FCommand.Value}";
            entity.FIp = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            entity.FHeaders=JsonConvert.SerializeObject(Request.Headers);
            var res = dataContext.DbConnection.ExecuteScalar<string>("P_RecordAccessLog", new { Params = JsonConvert.SerializeObject(entity) }, commandType: System.Data.CommandType.StoredProcedure);
            return Ok(new { FData = res });
        }

        [HttpGet]
        [Route("ec1")]
        public IActionResult TestGet([FromBody] dynamic entity)
        {
            //var command = $"P_{entity.FCommand.Value}";
            //entity.FIp = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            //entity.FHeaders=JsonConvert.SerializeObject(Request.Headers);
            //var res = dataContext.DbConnection.Execute("P_RecordAccessLog", new { Params = JsonConvert.SerializeObject(entity) }, commandType: System.Data.CommandType.StoredProcedure);
            return Ok(new { FMessage="ok" });
        }
    }
}

